<script setup lang="ts">
import { onMounted, ref } from "vue";
import {
  reqSkuCancelSale,
  reqSkuDelete,
  reqSkuDetail,
  reqSkuList,
  reqSkuOnSale,
} from "@/api/product/sku";
import { ElMessage, ElMessageBox } from "element-plus";
import { SkuList, SkuListItem } from "@/api/product/sku/type.ts";

let queryParams = ref({
  pageNum: 1, // 当前页码
  pageSize: 3, // 每页显示条数
});

// 定义总条数
let total = ref(30);

// 表格
let skuList: SkuList = ref([]);

// 抽屉是否展开
let drawer = ref(false);

// 抽屉方向
const direction = ref("rtl");

// 抽屉数据
// id: number;
// createTime?: string;
// updateTime?: string;
// spuId: number;
// price: number;
// skuName: string;
// skuDesc: string;
// weight: string;
// tmId: number;
// category3Id: number;
// skuDefaultImg: string;
// isSale: number;
let skuDetailForShow = ref<SkuListItem>({
  id: 0,
  spuId: 0,
  price: 0,
  skuName: "",
  skuDesc: "",
  weight: "",
  tmId: 0,
  category3Id: 0,
  skuDefaultImg: "",
  isSale: 0,
  skuAttrValueList: [],
  skuSaleAttrValueList: [],
});

// 获取SKU列表
const getSKUList = async () => {
  let result = await reqSkuList(
    queryParams.value.pageNum,
    queryParams.value.pageSize,
  );
  if (result.code) {
    skuList.value = result.data.records;
    total.value = result.data.total;
  } else {
    ElMessage.error("获取SKU列表失败！");
  }
};

// size-change：每页条数改变时触发
const handleSizeChange = (val: number) => {
  queryParams.value.pageSize = val; // 更新每页显示条数
  getSKUList();
};

// current-change：当前页改变时触发
const handleCurrentChange = (val: number) => {
  queryParams.value.pageNum = val; // 更新当前页码
  getSKUList();
};

// 上架
const onSale = async (row: SkuListItem) => {
  let result = await reqSkuOnSale(row.id);
  if (result.code) {
    ElMessage.success("上架成功！");
    getSKUList();
  } else {
    ElMessage.error("上架失败！");
  }
};

// 下架
const cancelSale = async (row: SkuListItem) => {
  let result = await reqSkuCancelSale(row.id);
  if (result.code) {
    ElMessage.success("下架成功！");
    getSKUList();
  } else {
    ElMessage.error("下架失败！");
  }
};

// 更新SKU
const updateSku = (row: SkuListItem) => {};

// 查看SKU详情
const showSkuDetail = async (row: SkuListItem) => {
  drawer.value = true;
  await getSkuDetail(row.id);
};

// 关闭抽屉
const handleClose = (done: () => void) => {
  // ElMessageBox.confirm('确认关闭吗？')
  //     .then(() => {
  //       done()
  //     })
  //     .catch(() => {
  //       // catch error
  //     })
  done();
};

// 获取sku详情
const getSkuDetail = async (skuId: number) => {
  let result = await reqSkuDetail(skuId);
  if (result.code == 200) {
    skuDetailForShow.value = result.data;
  }
};

//删除sku
const deleteSku = async (skuId: number) => {
  let result = await reqSkuDelete(skuId);
  if (result.code == 200) {
    ElMessage.success("删除成功！");
    getSKUList();
  } else {
    ElMessage.error("删除失败！");
  }
};

onMounted(() => {
  getSKUList();
});

defineOptions({
  name: "Sku",
});
</script>

<template>
  <el-card>
    <el-table border :data="skuList">
      <el-table-column
        type="index"
        label="序号"
        width="80px"
        align="center"
      ></el-table-column>
      <el-table-column
        label="名称"
        prop="skuName"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        label="描述"
        show-overflow-tooltip
        prop="skuDesc"
        width="300"
        align="center"
      ></el-table-column>
      <el-table-column
        label="图片"
        prop="skuDefaultImg"
        align="center"
        width="200"
      >
        <template #default="{ row }">
          <el-image
            style="width: 100px; height: 100px"
            :src="row.skuDefaultImg"
            fit="cover"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        label="重量"
        prop="weight"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        label="价格"
        prop="price"
        width="150"
        align="center"
      ></el-table-column>
      <!-- fixed 参数固定在 右侧   -->
      <el-table-column label="操作" width="300" fixed="right" align="center">
        <template #default="{ row }">
          <el-button
            v-if="row.isSale == 1"
            type="info"
            size="small"
            icon="arrow-down"
            @click="cancelSale(row)"
          ></el-button>
          <el-button
            v-else
            type="success"
            size="small"
            icon="arrow-up"
            @click="onSale(row)"
          >
          </el-button>

          <el-button
            type="primary"
            size="small"
            icon="edit"
            @click="updateSku(row)"
          ></el-button>
          <el-button
            type="info"
            size="small"
            icon="InfoFilled"
            @click="showSkuDetail(row)"
          >
          </el-button>
          <el-popconfirm
            title="确定删除吗？"
            confirmButtonText="确定"
            cancelButtonText="取消"
            icon="el-icon-question"
            @confirm="deleteSku(row.id)"
          >
            <template #reference>
              <el-button type="danger" size="small" icon="delete"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.pageNum"
      :page-sizes="[1, 3, 5, 10]"
      :page-size="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      class="float-right page"
    ></el-pagination>

    <el-drawer
      v-model="drawer"
      title="I am the title"
      :direction="direction"
      :before-close="handleClose"
    >
      <template #header> {{ skuDetailForShow.skuName }}的详细信息 </template>

      <template #default>
        <el-row class="margin-gap">
          <el-col :span="6"> 名称： </el-col>
          <el-col :span="18">
            {{ skuDetailForShow.skuName }}
          </el-col>
        </el-row>
        <el-row class="margin-gap">
          <el-col :span="6">描述：</el-col>
          <el-col :span="18">{{ skuDetailForShow.skuDesc }}</el-col>
        </el-row>
        <el-row class="margin-gap">
          <el-col :span="6">价格：</el-col>
          <el-col :span="18">{{ skuDetailForShow.price }}</el-col>
        </el-row>
        <el-row class="margin-gap">
          <el-col :span="6">平台属性：</el-col>
          <el-col :span="18">
            <el-tag
              v-for="skuAttrValue in skuDetailForShow.skuAttrValueList"
              class="margin-gap"
              >{{ skuAttrValue.valueName }}</el-tag
            >
          </el-col>
        </el-row>
        <el-row class="margin-gap">
          <el-col :span="6">销售属性：</el-col>
          <el-col :span="18">
            <el-tag
              v-for="skuSaleAttrValue in skuDetailForShow.skuSaleAttrValueList"
              class="margin-gap"
              >{{ skuSaleAttrValue.saleAttrValueName }}</el-tag
            >
          </el-col>
        </el-row>
        <el-row class="margin-gap">
          <el-col :span="6">图片：</el-col>
          <el-col :span="18">
            <el-carousel :interval="4000" type="card" height="200px">
              <el-carousel-item
                v-for="skuImage in skuDetailForShow.skuImageList"
                :key="skuImage.id"
              >
                <el-image
                  style="width: 100%; height: 100%"
                  :src="skuImage.imgUrl"
                  fit="contain"
                ></el-image>
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>

      <template #footer> </template>
    </el-drawer>
  </el-card>
</template>

<style scoped lang="scss">
.float-right {
  float: right;
}

.page {
  margin: 20px 0;
}

.margin-gap {
  margin: 10px;
}

//.el-carousel__item h3 {
//  color: #475669;
//  opacity: 0.75;
//  line-height: 100px;
//  margin: 0;
//  text-align: center;
//}
//
//.el-carousel__item:nth-child(2n) {
//  background-color: #99a9bf;
//}
//
//.el-carousel__item:nth-child(2n + 1) {
//  background-color: #d3dce6;
//}
</style>
